﻿// Decompiled with JetBrains decompiler
// Type: MCM.LightInject.ImmutableHashTableExtensions
// Assembly: MCMv5, Version=5.10.2.0, Culture=neutral, PublicKeyToken=null
// MVID: 7AB326B9-B2B4-41C2-9177-68E944071101
// Assembly location: D:\steam\steamapps\workshop\content\261550\2859238197\bin\Win64_Shipping_Client\MCMv5.dll

using System;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices;

#nullable disable
namespace MCM.LightInject
{
  [ExcludeFromCodeCoverage]
  internal static class ImmutableHashTableExtensions
  {
    [ExcludeFromCodeCoverage]
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    public static TValue Search<TKey, TValue>(
      this ImmutableHashTable<TKey, TValue> hashTable,
      TKey key)
    {
      int hashCode = key.GetHashCode();
      int index = hashCode & hashTable.Divisor - 1;
      ImmutableHashTree<TKey, TValue> immutableHashTree = hashTable.Buckets[index];
      while (immutableHashTree.Height != 0 && immutableHashTree.HashCode != hashCode)
        immutableHashTree = hashCode < immutableHashTree.HashCode ? immutableHashTree.Left : immutableHashTree.Right;
      if (immutableHashTree.Height != 0 && ((object) immutableHashTree.Key == (object) key || object.Equals((object) immutableHashTree.Key, (object) key)))
        return immutableHashTree.Value;
      if (immutableHashTree.Duplicates.Items.Length != 0)
      {
        foreach (KeyValue<TKey, TValue> keyValue in immutableHashTree.Duplicates.Items)
        {
          if ((object) keyValue.Key == (object) key || object.Equals((object) keyValue.Key, (object) key))
            return keyValue.Value;
        }
      }
      return default (TValue);
    }

    public static ImmutableHashTable<TKey, TValue> Add<TKey, TValue>(
      this ImmutableHashTable<TKey, TValue> hashTable,
      TKey key,
      TValue value)
    {
      return new ImmutableHashTable<TKey, TValue>(hashTable, key, value);
    }

    [ExcludeFromCodeCoverage]
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    internal static GetInstanceDelegate Search(
      this ImmutableHashTable<Type, GetInstanceDelegate> hashTable,
      Type key)
    {
      int hashCode = key.GetHashCode();
      int index = hashCode & hashTable.Divisor - 1;
      ImmutableHashTree<Type, GetInstanceDelegate> immutableHashTree = hashTable.Buckets[index];
      while (immutableHashTree.Height != 0 && immutableHashTree.HashCode != hashCode)
        immutableHashTree = hashCode < immutableHashTree.HashCode ? immutableHashTree.Left : immutableHashTree.Right;
      if (immutableHashTree.Height != 0 && (object) immutableHashTree.Key == (object) key)
        return immutableHashTree.Value;
      if (immutableHashTree.Duplicates.Items.Length != 0)
      {
        foreach (KeyValue<Type, GetInstanceDelegate> keyValue in immutableHashTree.Duplicates.Items)
        {
          if ((object) keyValue.Key == (object) key)
            return keyValue.Value;
        }
      }
      return (GetInstanceDelegate) null;
    }
  }
}
